<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>数组</title>
    <script src="../js/vue.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
            list-style: none;
        }

        .table {
            width: 400px;
            margin: 50px auto;
        }

        .table .tr .item {
            width: 100px;
            height: 40px;
            line-height: 40px;
            color: aquamarine;
            background-color: black;
            text-align: center;
            float: left;
        }
    </style>
</head>

<body>

    <div id="app">

        名字<input type="text" value="" @input="setUsername">
        电话<input type="text" value="" @input="setUsertel">
        <button @click="add">新增</button>

        <div class="table">
            <div class="tr">
                <div class="item">编号</div>
                <div class="item">名字</div>
                <div class="item">电话</div>
                <div class="item">操作</div>
            </div>

            <div class="tr" v-for="item in userList">
                <div class="item">{{item.id}}</div>
                <div class="item">{{item.username}}</div>
                <div class="item">{{item.usertel}}</div>
                <div class="item" @click="del(item.id)">删除</div>
            </div>

        </div>




    </div>


</body>

<script>

    new Vue({
        el: "#app",
        data: {
            username: "",
            usertel: "",


            list: [1, 2, 3, 4],
            userList: [{ id: 1, username: "qqq", usertel: 123456 }, { id: 2, username: "aaa", usertel: 123456 }, { id: 3, username: "bbb", usertel: 123456 }]
        },
        methods: {

            setUsername(e) {
                this.username = e.target.value;
            },

            setUsertel(e) {
                this.usertel = e.target.value;
            },

            add() {
                let ids = this.userList.map(item => item.id);
                ids.sort((a, b) => b - a);
                let id = ids[0] ? ids[0] + 1 : 1;


                this.userList.push({ id, username: this.username, usertel: this.usertel });
            },


            del(id) {
                this.userList = this.userList.filter(item => item.id != id);
            }
        }
    })



</script>

</html>